package com.mrd.shiptask_shipowner.fragments;

import java.util.HashMap;
import java.util.Map;

import com.lidroid.xutils.db.sqlite.CursorUtils.FindCacheSequence;
import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.serviecs.initdatadao.InitDatasDaoImpl;
import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;
import com.mrd.shiptask_shipowner.unit_tools.ButtonTool;
import com.mrd.shiptask_shipowner.unit_tools.FunctionOfActivity;
import com.mrd.shiptask_shipowner.unit_tools.HttpUrlConstant;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class ShipownerInfoFragment extends Fragment {

	private View view;
	private SharePreferenceUtil sp;
	private EditText infoNameEt, infoPhoneEt;
	private Button confirmBt, updateBt, cancelBt;
	private ProgressDialog progressDialog;
	private TextView infoNameT, infoPhoneT;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_shipowner_info, null);
		progressDialog = new ProgressDialog(getActivity());
		sp = new SharePreferenceUtil(getActivity(), HttpConstant.SAVE_USER);
		return view;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		initShipOwnerInformation();
		initDataView(view);
	}

	private void initDataView(View view) {
		// TODO Auto-generated method stub
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		infoNameT = (TextView) view.findViewById(R.id.infoNameT);
		infoPhoneT = (TextView) view.findViewById(R.id.infoPhonT);

		infoNameEt = (EditText) view.findViewById(R.id.infoNameEt);
		infoPhoneEt = (EditText) view.findViewById(R.id.infoPhonEt);

		confirmBt = (Button) view.findViewById(R.id.confirmBt);
		updateBt = (Button) view.findViewById(R.id.updateBt);
		cancelBt = (Button) view.findViewById(R.id.cancelBt);
		// 初始状态
		cancelBt.setVisibility(View.GONE);
		confirmBt.setVisibility(View.GONE);
		updateBt.setVisibility(View.VISIBLE);
		infoNameEt.setVisibility(View.GONE);
		infoPhoneEt.setVisibility(View.GONE);
		infoNameT.setVisibility(View.VISIBLE);
		infoPhoneT.setVisibility(View.VISIBLE);

		confirmBt.setOnClickListener(ocl);
		updateBt.setOnClickListener(ocl);
		cancelBt.setOnClickListener(ocl);
		initViewStatus();
		ButtonTool
				.onClickButtonStyle(getActivity(), new Button[] { confirmBt });

	}

	private void initViewStatus() {
		// TODO Auto-generated method stub
		infoNameT.setText(sp.getShipOwnerName());
		infoPhoneT.setText(sp.getShipOwnerPhone());
		infoNameEt.setText(sp.getShipOwnerName());
		infoPhoneEt.setText(sp.getShipOwnerPhone());
	}

	private final OnClickListener ocl = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.confirmBt:
				new AlertDialog.Builder(getActivity())
						.setMessage("您是否确认修改内容？")
						.setTitle("提示")
						.setIcon(android.R.drawable.ic_dialog_info)
						.setPositiveButton("是",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										progressDialog
												.setMessage("正在加载，请稍候...");
										progressDialog.show();
										Map<String, Object> param = new HashMap<String, Object>();
										param.put("ship_id", sp.getShip_id());
										param.put("ship_owner", infoNameEt
												.getText().toString());
										param.put("owner_phone", infoPhoneEt
												.getText().toString());
										InitDatasDaoImpl.confirmShipOwnerInfo(
												getActivity(), param, handler);
									}
								}).setNegativeButton("取消", null).show();
				break;
			case R.id.updateBt:
				cancelBt.setVisibility(View.VISIBLE);
				confirmBt.setVisibility(View.VISIBLE);
				updateBt.setVisibility(View.GONE);
				infoNameEt.setVisibility(View.VISIBLE);
				infoPhoneEt.setVisibility(View.VISIBLE);
				infoNameT.setVisibility(View.GONE);
				infoPhoneT.setVisibility(View.GONE);
				break;
			case R.id.cancelBt:
				cancelBt.setVisibility(View.GONE);
				confirmBt.setVisibility(View.GONE);
				updateBt.setVisibility(View.VISIBLE);
				infoNameEt.setVisibility(View.GONE);
				infoPhoneEt.setVisibility(View.GONE);
				infoNameT.setVisibility(View.VISIBLE);
				infoPhoneT.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}
		}
	};

	private void initShipOwnerInformation() {
		// TODO Auto-generated method stub
		InitDatasDaoImpl.getShipOwnerInformation(getActivity(), handler);
	}

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			try {
				Bundle data = msg.getData();
				if (!data.isEmpty()) {
					switch (msg.what) {
					case HttpUrlConstant.INIT_CONTACT:
						if (data.getBoolean("checkPassOrNot")) {
							initView();
						}
						break;
					case HttpUrlConstant.SHIP_OWNER:
						progressDialog.dismiss();
						FunctionOfActivity.makeToaskShow(getActivity(),
								data.getString("msg"));
						if (data.getBoolean("isPassOrNot")) {
							onStart();
						}
						break;
					default:
						break;
					}
				}
			} catch (Exception e) {
			}
		};
	};
}
